package win.smartown.kotlin.grammar

/**
 * Created by smartown on 2017/6/5.
 *
 * 使用ranges
 */
fun main(args: Array<String>) {
    testIn()
    testOut()
    testRecycle()
    testStep()
}

//使用in操作符检查数值是否在某个范围内
fun testIn(): Unit {
    val x = 10
    val y = 9
    if (x in 1..y) {
        println("$x fits in range ${1..y}")
    } else {
        println("$x not fits in range ${1..y}")
    }
    if (x in 1..y + 1) {
        println("$x fits in range ${1..y + 1}")
    } else {
        println("$x not fits in range ${1..y + 1}")
    }
}

//检查数值是否在范围外
fun testOut(): Unit {
    val list = listOf("a", "b", "c")
    if (-1 !in 0..list.lastIndex) {
        println("-1 is out of range")
    }
    if (list.size !in list.indices) {
        println("list size is out of valid list indices range too")
    }
}

//在范围内迭代
fun testRecycle() {
    for (x in 1..5) {
        print("$x ")
    }
    println()
}

//使用步进
fun testStep(): Unit {
    for (x in 1..10 step 2) {
        print("$x ")
    }
    println()
    for (x in 9 downTo 0 step 3) {
        print("$x ")
    }
    println()
}